const { SuccessModel, ErrorModel } = require("../../model/resModel");
const { getList, getOnSaleList, getDetail, createNewProduct, updateProduct, changeProduct, deleteProduct, selectProduct,selectOnSaleProduct, selectCategory, searchCategory } = require('../../controllers/admin/product');

const handleProductRoute = (req, res) => {
    const method = req.method;
    const id = req.query.id;
    const productData = req.body;

    //新建产品
    if (method === 'POST' && req.path === '/api/product/new') {
        const newProductDataPromise = createNewProduct(productData);
        return newProductDataPromise.then(newProductData => {
            return new SuccessModel(newProductData);
        })
        
    }

    //删除产品
    if (method === 'POST' && req.path === '/api/product/delete') {
        const deletedProductPromise = deleteProduct(id);
        return deletedProductPromise.then(deletedProductData => {
            if (deletedProductData) {
                return new SuccessModel(`删除产品${id}成功`);
            } else {
                return new ErrorModel('删除失败');
            }
        })
        
    }

    //更新产品信息
    if (method === 'POST' && req.path === '/api/product/change') {
        const changeProductPromise = changeProduct(id, productData);
        return changeProductPromise.then(changeProductData => {
            if (changeProductData) {
                return new SuccessModel(`更新产品在售状态${id}成功`);
            } else {
                return new ErrorModel('更新产品在售状态失败');
            }
        })
    }

    //更新产品
    if (method === 'POST' && req.path === '/api/product/update') {
        const updatedProductPromise = updateProduct(id, productData);
        return updatedProductPromise.then(updatedProductData => {
            if (updatedProductData) {
                return new SuccessModel(`更新产品${id}成功`);
            } else {
                return new ErrorModel('更新产品失败');
            }
        })
        
    }

    //获取产品列表
    if (method === 'GET' && req.path === '/api/product/list') {
        const listDataPromise = getList();
        return listDataPromise.then(listData => {
            return new SuccessModel(listData,'产品列表');
        });
    }

    //前台获取产品列表
    if (method === 'GET' && req.path === '/api/product/salelist') {
        const listDataPromise = getOnSaleList();
        return listDataPromise.then(listData => {
            return new SuccessModel(listData, '产品列表');
        });
    }

    //获取产品详情
    if (method === 'GET' && req.path === '/api/product/detail') {
        const detailDataPromise = getDetail(id);
        return detailDataPromise.then(detailData => {
            return new SuccessModel(detailData[0],`产品${id}的详情信息`);
        })
    }

    // 产品模糊搜索
    if (method === 'POST' && req.path === '/api/product/select') {
        const selectProductPromise = selectProduct(productData);
        return selectProductPromise.then(selectProduct => {
            return new SuccessModel(selectProduct,`检索到的产品信息`);
        })
    }

    // 前台产品模糊搜索
    if (method === 'POST' && req.path === '/api/product/saleselect') {
        const selectProductPromise = selectOnSaleProduct(productData);
        return selectProductPromise.then(selectProduct => {
            return new SuccessModel(selectProduct, `检索到的产品信息`);
        })
    }
    //查取类型列表
    if (method === 'POST' && req.path === '/api/product/category') {
        const selectCategoryPromise = selectCategory(productData);
        return selectCategoryPromise.then(selectCategory => {
            return new SuccessModel(selectCategory, `检索到的产品信息`);
        })
    }

    //类型搜索
    if (method === 'POST' && req.path === '/api/product/search') {
        const searchCategoryPromise = searchCategory(productData);
        return searchCategoryPromise.then(searchCategory => {
            return new SuccessModel(searchCategory, `检索到的产品信息`);
        })
    }

}

module.exports = handleProductRoute;